using System;
using System.Data;
using System.Data.SQLite;
using System.IO;

namespace QntPlatform.Db
{
    public class SqliteBak
    {
        public void BackUpDateBase()
        {
            string backUpFileName = "configMng.backUpFileName";
            string backUpPath="";
            int index = backUpFileName.IndexOf('.');
            string destDBFileNameProcess = backUpFileName.Insert(index, "_process");
            SQLiteConnection cnDest = new SQLiteConnection(@"Data Source=" + backUpPath + @"\" + destDBFileNameProcess);
            SQLiteConnection cnSource = new SQLiteConnection(@"Data Source=..\DataBase\" + backUpFileName);
            try
            {
                cnDest.Open();
                cnSource.Open();

                cnSource.BackupDatabase(cnDest, "main", "main", -1, null, 0);
                cnDest.Close();
                cnSource.Close();

                if (File.Exists(backUpPath + @"\" + destDBFileNameProcess))
                {
                   var destDBFileName = backUpFileName.Insert(index, DateTime.Now.ToString("yyyyMMddHHmmss"));
                    File.Move(backUpPath + @"\" + destDBFileNameProcess, backUpPath + @"\" + destDBFileName);
                }
            }
            catch(Exception e)
            {
               // LogHelper.WiteLog("SQLiteBackUpService backup faild!");
                if (cnDest.State == ConnectionState.Open)
                {
                    cnDest.Close();
                }
                if (cnSource.State == ConnectionState.Open)
                {
                    cnSource.Close();
                }

            }

        }
    }
}
